package com.kdtech.analyse.news;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.AnalyseNews;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.entity.data.NewsPaperMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 * http://szb.keq0475.com 科尔泌都市报
 * @author abc
 *
 */
public class Keq0475NewsPaperAnalyse implements AnalyseNews {

	
	
	public boolean isDetailPage(String url) {
		boolean bRet = false;
		String[] regex = {
				"http://news.keq0475.com/newsshow-[0-9]*.html",
				};
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {		
		NewsMeta newspaper = new NewsMeta();
				
		String html = urlMeta.getHtml();
		String url = urlMeta.getUrl();		
						
		newspaper.setUrl(url);		
		String title = null;
		String content = null;
		String datestr= null;
		Long date = null;
		Document doc = Jsoup.parse(html);
		title =doc.select("h1").text();
		
		content =HtmlCleaner.getContentHtml(url,doc.select("div.conter_show"));
		
		datestr= doc.select("div.Title_h1").text();
		date=DateUtils.matchDate(datestr);
		
		newspaper.setTitle(StringUtils.trimSpace(title));
		newspaper.setContent(StringUtils.trimSpace(content));
		newspaper.setDate(date);
		return newspaper;
	}
	
	
	public static void main(String[] args) {
		Keq0475NewsPaperAnalyse a = new Keq0475NewsPaperAnalyse();
		String url = "http://news.keq0475.com/newsshow-28206.html";
		System.out.println(a.isDetailPage(url));
		UrlMeta meta = CrawlHTML.responseToURL(url);
		NewsMeta parserHtml = a.parserHtml(meta);
		System.out.println(parserHtml);
	}


	
	public boolean isNeedUpdate() {
		// TODO Auto-generated method stub
		return false;
	}

	
	public NewsMeta Update(NewsMeta meta) {
		// TODO Auto-generated method stub
		return null;
	}
}
